package com.cn.goodsadmin.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * Created with IntelliJ IDEA.
 * 零售网金平台架构部网关组
 * User: fly
 * Date: 2018-09-27
 * Time: 上午11:15
 */

@Component
@Order(-99999)
@Slf4j
public class CorsFilter implements Filter {


    //private SSOFilterHandler ssoFilterHandle=new SSOFilterHandler();
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest reqs = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        System.out.println("origin=" + reqs.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Origin", reqs.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With, token,sessiontick");
        chain.doFilter(reqs, res);
    }

    public void init(FilterConfig filterConfig) {

    }

    public void destroy() {

    }

}
